<?php


namespace App\controllers\admin;


use App\controllers\Base;
use Helper\Config;
use Helper\Output;
use Helper\Mysql;
use Firebase\JWT\JWT;
use Helper\Reg;

class WorkOrder extends Base
{


    public function index($request, $response){

        $page = $request->post['page']?:1;
        $pagesize = $request->post['page_size']?:20;

        $list = Mysql::getInstance()
            ->where('wo.platform_id',$request->post['platform_id'])
            ->join('admin_user as au','wo.auid=au.id','left')
            ->get('work_order as wo',[($page-1)*$pagesize,$pagesize],'wo.id,wo.title,wo.content,wo.state,wo.create_at,wo.remark,au.nick');
        $total = Mysql::getInstance()
            ->where('platform_id',$request->post['platform_id'])
            ->getValue('work_order','count(*)');
        return Output::json(200,['list'=> $list ,'total'=> $total]);

    }

    public function create($request, $response){

        $data = $request->post;
        $data['remark']  = $request->post['remark'];
        $data['auid']    = $this->getUserId();
        $data['platform_id']  = $request->post['platform_id']?:$this->getUser()->platform_id;
        $data['create_at'] = time();

        $res = Mysql::getInstance()->insert('work_order',$data);
        if($res){
            return Output::json(200);
        }
        return Output::json(400);
    }


    public function edit($request, $response){

        $data = $request->post;
        $id = $request->post['id'];
        unset($request->post['id']);
        $data['remark']  = $request->post['remark'];

        $res = Mysql::getInstance()->where('id',$id)->update('work_order',$data);
        if($res){
            return Output::json(200);
        }
        return Output::json(400);
    }

}